import util from '@/libs/util';
import getDate from '@/util/getDate.js';
import Cookies from 'js-cookie';

export default {
    name: 'exportable-table',
    data () {
        return {
            columns: [{
                title: '序号',
                key: 'rownum',
                align: 'center',
                width: 60,
                render: (h, params) => {
                return h('div', {}, ((this.pageIndex - 1) * this.pageSize + params.index + 1));
    }
    }, {
            title: '姓名',
                key: 'name'
        }, {
            title: '开户银行',
                key: 'depositBank'
        }, {
            title: '手机号',
                key: 'cellphone'
        },  {title: '操作',
            key: 'action',
            width: 200,
            align: 'center',
            render: (h, params) => {
            return h('div', [
                h('Button', {
                    props: {
                        type: 'primary',
                        size: 'small'
                    },
                    style: {
                        marginRight: '5px'
                    },
                    on: {
                        click: () => {
                        this.isRead = true;
            this.showEdit(params.row);
        }
        }
        }, '查看'),
            h('Button', {
                props: {
                    type: 'primary',
                    size: 'small'
                },
                style: {
                    marginRight: '5px'
                },
                on: {
                    click: () => {
                    this.isRead = false;
            this.showEdit(params.row);
        }
        }
        }, '修改'),
            h('Button', {
                props: {
                    type: 'error',
                    size: 'small'
                },
                on: {
                    click: () => {
                    this.delete(params.row.id);
        }
        }
        }, '删除')
        ]);
        }
        }],
        data: [],
            formData: {},
        query: {
            name: ''
        },
        total: 0,
            pageIndex: 1,
            pageSize: 10,
            orgTree: [],
            majorTree: [],
            ids: [],
            baseuser: JSON.parse(Cookies.get('baseUser')),
            editModal: false,
            isRead: false,

    };
    },
    mounted () {
        this.loadList();
    },
    methods: {
        loadList () {
            util.ajax.post('/busine/tescher/info/list', util.toFormData({
                page: this.pageIndex,
                size: this.pageSize,
                name: this.query.name
            })).then(res => {
                this.data = res.data.data.list;
            this.total = res.data.data.total;
        });
        },
        refresh () {
            this.query={
                name: ''
            };
            this.into();
        },
        showAdd () {
            this.isRead = false;
            this.formData = {};
            this.editModal = true;
        },
        showEdit (obj) {
            this.formData = {};
            util.ajax.post('/busine/tescher/info/detail', util.toFormData({
                id: obj.id
            })).then(res => {
                if (res.data.data) {
                this.formData = res.data.data;
                this.editModal = true;
            } else {
                this.$Modal.error({
                    title: '警告',
                    content: '出错了'
                });
            }
        });
        },
        delete (id) {
            this.$Modal.confirm({
                title: '提示',
                content: '<p>确定删除？</p>',
                onOk: () => {
                util.ajax.post('/busine/tescher/info/delete', util.toFormData({
                id: id
            })).then(res => {
                if (res.data.message === 'SUCCESS') {
                this.$Message.success('删除成功！');
                this.loadList();
            }
        });
        }
        });
        },
        save () {
            let mobilePhonereg = /^(((17[0-9]{1})|(13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$/;
            let formData = {};
            for (let key in this.formData) {
                formData[key] = this.formData[key];
            }
            if (!formData.name) {
                this.$Modal.error({content: '教师名称不能为空！'});
                return;
            }
            if (!formData.cellphone) {
                this.$Modal.error({content: '手机号不能为空！'})
                return false;
            }
            if (!mobilePhonereg.test(formData.cellphone)) {
                this.$Modal.error({content: '请输入有效的手机号！'})
                return false;
            }
            util.ajax.post('/busine/tescher/info/save', util.toFormData(formData)).then(res => {
                if (res.data.message === 'SUCCESS') {
                this.$Message.success('保存成功！');
                this.loadList();
                this.editModal = false;
            } else {
                this.$Modal.error({
                    content: res.data.message || '未知错误！'
                });
            }
        });
        },
    }
}